import { AMF } from '../../src/flv/amf'

describe('AMF', () => {

  test('AMF', () => {
    const data = new Uint8Array([
      2, 0, 10, 111, 110, 77, 101, 116, 97, 68, 97, 116,
      97, 8, 0, 0, 0, 13, 0, 8, 100, 117, 114, 97,
      116, 105, 111, 110, 0, 64, 34, 34, 77, 210, 241, 169,
      252, 0, 5, 119, 105, 100, 116, 104, 0, 64, 120, 64,
      0, 0, 0, 0, 0, 0, 6, 104, 101, 105, 103, 104,
      116, 0, 64, 114, 192, 0, 0, 0, 0, 0, 0, 13,
      118, 105, 100, 101, 111, 100, 97, 116, 97, 114, 97, 116,
      101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
      102, 114, 97, 109, 101, 114, 97, 116, 101, 0, 64, 46, 0, 0, 0, 0,
      0, 0, 0, 12, 118, 105, 100, 101, 111, 99, 111, 100,
      101, 99, 105, 100, 0, 64, 28, 0, 0, 0, 0, 0,
      0, 0, 13, 97, 117, 100, 105, 111, 100, 97, 116, 97,
      114, 97, 116, 101, 0, 64, 95, 64, 0, 0, 0, 0,
      0, 0, 15, 97, 117, 100, 105, 111, 115, 97, 109, 112,
      108, 101, 114, 97, 116, 101, 0, 64, 229, 136, 128, 0,
      0, 0, 0, 0, 15, 97, 117, 100, 105, 111, 115, 97,
      109, 112, 108, 101, 115, 105, 122, 101, 0, 64, 48, 0, 0, 0, 0, 0,
      0, 0, 6, 115, 116, 101, 114, 101, 111, 1, 1, 0,
      12, 97, 117, 100, 105, 111, 99, 111, 100, 101, 99, 105,
      100, 0, 64, 36, 0, 0, 0, 0, 0, 0, 0, 7,
      101, 110, 99, 111, 100, 101, 114, 2, 0, 13, 76, 97,
      118, 102, 53, 56, 46, 55, 54, 46, 49, 48, 48, 0,
      8, 102, 105, 108, 101, 115, 105, 122, 101, 0, 65, 25,
      224, 172, 0, 0, 0, 0, 0, 0, 9
    ])

    const result = AMF.parse(data)

    expect(result).toEqual({
      onMetaData: {
        duration: 9.067,
        width: 388,
        height: 300,
        videodatarate: 0,
        framerate: 15,
        videocodecid: 7,
        audiodatarate: 125,
        audiosamplerate: 44100,
        audiosamplesize: 16,
        stereo: true,
        audiocodecid: 10,
        encoder: 'Lavf58.76.100',
        filesize: 423979
      }
    })
  })

  test('keyframes and filepositions', () => {
    const data = new Uint8Array([2, 0, 10, 111, 110, 77, 101, 116, 97, 68, 97, 116, 97, 8, 0, 0, 0, 25, 0, 15, 109, 101, 116, 97, 100, 97, 116, 97, 99, 114, 101, 97, 116, 111, 114, 2, 0, 51, 89, 101, 116, 32, 65, 110, 111, 116, 104, 101, 114, 32, 77, 101, 116, 97, 100, 97, 116, 97, 32, 73, 110, 106, 101, 99, 116, 111, 114, 32, 102, 111, 114, 32, 70, 76, 86, 32, 45, 32, 86, 101, 114, 115, 105, 111, 110, 32, 49, 46, 57, 0, 12, 104, 97, 115, 75, 101, 121, 102, 114, 97, 109, 101, 115, 1, 1, 0, 8, 104, 97, 115, 86, 105, 100, 101, 111, 1, 1, 0, 8, 104, 97, 115, 65, 117, 100, 105, 111, 1, 1, 0, 11, 104, 97, 115, 77, 101, 116, 97, 100, 97, 116, 97, 1, 1, 0, 12, 99, 97, 110, 83, 101, 101, 107, 84, 111, 69, 110, 100, 1, 1, 0, 8, 100, 117, 114, 97, 116, 105, 111, 110, 0, 64, 86, 128, 0, 0, 0, 0, 0, 0, 8, 100, 97, 116, 97, 115, 105, 122, 101, 0, 65, 98, 11, 6, 192, 0, 0, 0, 0, 9, 118, 105, 100, 101, 111, 115, 105, 122, 101, 0, 65, 94, 39, 99, 128, 0, 0, 0, 0, 9, 102, 114, 97, 109, 101, 114, 97, 116, 101, 0, 64, 57, 8, 136, 136, 136, 136, 137, 0, 13, 118, 105, 100, 101, 111, 100, 97, 116, 97, 114, 97, 116, 101, 0, 64, 133, 96, 33, 244, 159, 73, 244, 0, 12, 118, 105, 100, 101, 111, 99, 111, 100, 101, 99, 105, 100, 0, 64, 28, 0, 0, 0, 0, 0, 0, 0, 5, 119, 105, 100, 116, 104, 0, 64, 138, 192, 0, 0, 0, 0, 0, 0, 6, 104, 101, 105, 103, 104, 116, 0, 64, 126, 0, 0, 0, 0, 0, 0, 0, 9, 97, 117, 100, 105, 111, 115, 105, 122, 101, 0, 65, 55, 90, 212, 0, 0, 0, 0, 0, 13, 97, 117, 100, 105, 111, 100, 97, 116, 97, 114, 97, 116, 101, 0, 64, 96, 34, 30, 91, 105, 142, 100, 0, 12, 97, 117, 100, 105, 111, 99, 111, 100, 101, 99, 105, 100, 0, 64, 36, 0, 0, 0, 0, 0, 0, 0, 15, 97, 117, 100, 105, 111, 115, 97, 109, 112, 108, 101, 114, 97, 116, 101, 0, 64, 8, 0, 0, 0, 0, 0, 0, 0, 15, 97, 117, 100, 105, 111, 115, 97, 109, 112, 108, 101, 115, 105, 122, 101, 0, 63, 240, 0, 0, 0, 0, 0, 0, 0, 6, 115, 116, 101, 114, 101, 111, 1, 1, 0, 8, 102, 105, 108, 101, 115, 105, 122, 101, 0, 65, 98, 11, 114, 224, 0, 0, 0, 0, 13, 108, 97, 115, 116, 116, 105, 109, 101, 115, 116, 97, 109, 112, 0, 64, 86, 128, 0, 0, 0, 0, 0, 0, 21, 108, 97, 115, 116, 107, 101, 121, 102, 114, 97, 109, 101, 116, 105, 109, 101, 115, 116, 97, 109, 112, 0, 64, 86, 128, 0, 0, 0, 0, 0, 0, 20, 108, 97, 115, 116, 107, 101, 121, 102, 114, 97, 109, 101, 108, 111, 99, 97, 116, 105, 111, 110, 0, 65, 98, 11, 112, 96, 0, 0, 0, 0, 9, 107, 101, 121, 102, 114, 97, 109, 101, 115, 3, 0, 13, 102, 105, 108, 101, 112, 111, 115, 105, 116, 105, 111, 110, 115, 10, 0, 0, 0, 13, 0, 64, 139, 8, 0, 0, 0, 0, 0, 0, 64, 141, 200, 0, 0, 0, 0, 0, 0, 65, 31, 197, 148, 0, 0, 0, 0, 0, 65, 51, 102, 254, 0, 0, 0, 0, 0, 65, 65, 35, 54, 0, 0, 0, 0, 0, 65, 74, 50, 167, 0, 0, 0, 0, 0, 65, 80, 158, 242, 192, 0, 0, 0, 0, 65, 83, 41, 184, 128, 0, 0, 0, 0, 65, 88, 26, 87, 64, 0, 0, 0, 0, 65, 91, 91, 233, 128, 0, 0, 0, 0, 65, 94, 78, 209, 192, 0, 0, 0, 0, 65, 97, 33, 64, 96, 0, 0, 0, 0, 65, 98, 11, 112, 96, 0, 0, 0, 0, 5, 116, 105, 109, 101, 115, 10, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 20, 40, 245, 194, 143, 92, 41, 0, 64, 42, 40, 245, 194, 143, 92, 41, 0, 64, 51, 245, 194, 143, 92, 40, 246, 0, 64, 57, 0, 0, 0, 0, 0, 0, 0, 64, 65, 128, 0, 0, 0, 0, 0, 0, 64, 70, 128, 0, 0, 0, 0, 0, 0, 64, 75, 128, 0, 0, 0, 0, 0, 0, 64, 80, 64, 0, 0, 0, 0, 0, 0, 64, 82, 12, 204, 204, 204, 204, 205, 0, 64, 84, 140, 204, 204, 204, 204, 205, 0, 64, 86, 128, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 9])
    const result = AMF.parse(data)

    expect(result).toEqual({
      onMetaData: {
        metadatacreator: 'Yet Another Metadata Injector for FLV - Version 1.9',
        hasKeyframes: true,
        hasVideo: true,
        hasAudio: true,
        hasMetadata: true,
        canSeekToEnd: true,
        duration: 90,
        datasize: 9459766,
        videosize: 7904654,
        framerate: 25.033333333333335,
        videodatarate: 684.016579861111,
        videocodecid: 7,
        width: 856,
        height: 480,
        audiosize: 1530580,
        audiodatarate: 129.06620569817017,
        audiocodecid: 10,
        audiosamplerate: 3,
        audiosamplesize: 1,
        stereo: true,
        filesize: 9460631,
        lasttimestamp: 90,
        lastkeyframetimestamp: 90,
        lastkeyframelocation: 9460611,
        keyframes: {
            filepositions: [865, 953, 520549, 1271550, 2246252, 3433806, 4357067, 5023458, 6318429, 7172006, 7945031, 8980995, 9460611],
            times: [0, 0, 5.04, 13.08, 19.96, 25, 35, 45, 55, 65, 72.2, 82.2, 90]
          }
      }
    })
  })

})
